﻿using AMC.SubstitutionCache.Services;

using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;

namespace AMC.SubstitutionCache.Handlers
{
    public class SubstitutionCacheInvalidationHandler : ContentHandler
    {
        private readonly ISubstitutionCacheStorageProvider _cache;

        public SubstitutionCacheInvalidationHandler(ISubstitutionCacheStorageProvider cache)
        {
            _cache = cache;

            // evict cached items based on modified content part when updated
            OnUpdated<IContent>((context, part) => Invalidate(part));
        }

        private void Invalidate(IContent content)
        {
            foreach (var part in content.ContentItem.Parts)
            {
                _cache.RemoveByPart(part);
            }
        }
    }
}